﻿using System;
using Firestarter.Test.Base;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    [TestFixture]
    public class TestDMPropertyAutoIncrementingMustBeOfTypeInt
    {
        [SetUp]
        public void SetupTest()
        {
            //Runs every time that any testmethod is executed
            TestUtilsDomain.SetupTest();
            BORegistry.DataAccessor = new DataAccessorInMemory();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            TestUtilsDomain.SetupFixture();
        }

        [TearDown]
        public void TearDownTest()
        {
            //runs every time any testmethod is complete
        }

        #region TestDMPropertyAutoIncrementingMustBeOfTypeInt

        [Test]
        public void Test_Contruct()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMPropertyAutoIncrementingMustBeOfTypeInt();
            //---------------Test Result -----------------------
            Assert.IsNotNull(rule);
        }

        [Test]
        public void Test_Construct_ShouldSupport_IBusinessObjectRule()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMPropertyAutoIncrementingMustBeOfTypeInt();
            //---------------Test Result -----------------------
            TestUtilsShared.AssertIsInstanceOf<IBusinessObjectRule>(rule);
        }

        [Test]
        public void Test_Name_ShouldBeCorrect()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            IBusinessObjectRule rule = new DMPropertyAutoIncrementingMustBeOfTypeInt();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            string name = rule.Name;
            //---------------Test Result -----------------------
            Assert.AreEqual("DMPropertyAutoIncrementingMustBeOfTypeInt", name);
        }

        [Test]
        public void Test_Error()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMPropertyAutoIncrementingMustBeOfTypeInt();
            //---------------Test Result -----------------------
            Assert.AreEqual(ErrorLevel.Error, rule.ErrorLevel);
        }

        [Test]
        public void Test_Message()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            dmProperty.Class = dmClass;
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMPropertyAutoIncrementingMustBeOfTypeInt();
            rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            StringAssert.Contains("The Property '" + dmProperty.ToString() + "' on class '" + dmProperty.Class.ToString()
                + "' with Property Type '" + dmProperty.PropertyType.ToString() + "' cannot be AutoIncrementing", rule.Message);
        }

//        [Test]
//        public void Test_DMPropertyPropertyTypeIntCanBeOnlyAutoincrement_WhenDMPropertyNull_ShouldRaiseError()
//        {
//            //---------------Set up test pack-------------------
//            //---------------Assert Precondition----------------
//            //---------------Execute Test ----------------------
//            try
//            {
//                new DMPropertyAutoIncrementingMustBeOfTypeInt(null);
//                Assert.Fail("expected ArgumentNullException");
//            }
//            //---------------Test Result -----------------------
//            catch (ArgumentNullException ex)
//            {
//                StringAssert.Contains("Value cannot be null", ex.Message);
//                StringAssert.Contains("dmProperty", ex.ParamName);
//            }
//        }

        [Test]
        public void Test_IsValid_WhenPropertyTypeStringAndAutoIncrementTrue_ShouldReturnFalse()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            string propType = "String";
            DMProperty dmProperty = GetDMProperty(dmClass, propType);
            SetAutoIncrementing(dmProperty, true);
            IBusinessObjectRule rule = new DMPropertyAutoIncrementingMustBeOfTypeInt();
            //---------------Assert Precondition----------------
            Assert.AreEqual("String", propType);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            TestUtilsShared.AssertStringNotEmpty(rule.Message, "rule.Message");
        }

        [Test]
        public void Test_IsValid_WhenPropertyTypeStringAndAutoIncrementFalse_ShouldReturnTrue()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            string propType = "String";
            DMProperty dmProperty = GetDMProperty(dmClass, propType);
            SetAutoIncrementing(dmProperty, false);
            IBusinessObjectRule rule = new DMPropertyAutoIncrementingMustBeOfTypeInt();
            //---------------Assert Precondition----------------
            Assert.AreEqual("String", propType);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_WhenPropertyTypeDecimalAndAutoIncrementTrue_ShouldReturnFalse()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            string propType = "Decimal";
            DMProperty dmProperty = GetDMProperty(dmClass, propType);
            SetAutoIncrementing(dmProperty, true);
            IBusinessObjectRule rule = new DMPropertyAutoIncrementingMustBeOfTypeInt();
            //---------------Assert Precondition----------------
            Assert.AreEqual("Decimal", propType);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            TestUtilsShared.AssertStringNotEmpty(rule.Message, "rule.Message");
        }

        [Test]
        public void Test_IsValid_WhenPropertyTypeDecimalAndAutoIncrementFalse_ShouldReturnTrue()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            string propType = "Decimal";
            DMProperty dmProperty = GetDMProperty(dmClass, propType);
            SetAutoIncrementing(dmProperty, false);
            IBusinessObjectRule rule = new DMPropertyAutoIncrementingMustBeOfTypeInt();
            //---------------Assert Precondition----------------
            Assert.AreEqual("Decimal", propType);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_WhenPropertyTypeNotIntAndAutoIncrementTrue_ShouldReturnFalse()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            string propType = TestUtilsShared.GetRandomString();
            DMProperty dmProperty = GetDMProperty(dmClass, propType);
            SetAutoIncrementing(dmProperty, true);
            IBusinessObjectRule rule = new DMPropertyAutoIncrementingMustBeOfTypeInt();
            //---------------Assert Precondition----------------
            Assert.IsFalse(propType.StartsWith("Int"));
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            TestUtilsShared.AssertStringNotEmpty(rule.Message, "rule.Message");
        }

        [Test]
        public void Test_IsValid_WhenPropertyTypeNotIntAndAutoIncrementFalse_ShouldReturnTrue()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            string propType = TestUtilsShared.GetRandomString();
            DMProperty dmProperty = GetDMProperty(dmClass, propType);
            SetAutoIncrementing(dmProperty, false);
            IBusinessObjectRule rule = new DMPropertyAutoIncrementingMustBeOfTypeInt();
            //---------------Assert Precondition----------------
            Assert.IsFalse(propType.StartsWith("Int"));
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }


        [Test]
        public void Test_IsValid_WhenPropertyTypeInt32AndAutoIncrementTrue_ShouldReturnTrue()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            string propType = "Int32";
            DMProperty dmProperty = GetDMProperty(dmClass, propType);
            SetAutoIncrementing(dmProperty, true);
            IBusinessObjectRule rule = new DMPropertyAutoIncrementingMustBeOfTypeInt();
            //---------------Assert Precondition----------------
            Assert.AreEqual("Int32", propType);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_WhenPropertyTypeInt64AndAutoIncrementTrue_ShouldReturnTrue()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            string propType = "Int64";
            DMProperty dmProperty = GetDMProperty(dmClass, propType);
            SetAutoIncrementing(dmProperty, true);
            IBusinessObjectRule rule = new DMPropertyAutoIncrementingMustBeOfTypeInt();
            //---------------Assert Precondition----------------
            Assert.AreEqual("Int64", propType);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }


        [Test]
        public void Test_IsValid_WhenPropertyTypeNameIsNull_ShouldReturnTrue()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            string propType = null;
            DMProperty dmProperty = GetDMProperty(dmClass, propType);
            IBusinessObjectRule rule = new DMPropertyAutoIncrementingMustBeOfTypeInt();
            //---------------Assert Precondition----------------
            Assert.IsNull(propType);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_WhenPropertyTypeNameIsEmpty_ShouldReturnTrue()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            string propType = "";
            DMProperty dmProperty = GetDMProperty(dmClass, propType);
            IBusinessObjectRule rule = new DMPropertyAutoIncrementingMustBeOfTypeInt();
            //---------------Assert Precondition----------------
            Assert.IsNullOrEmpty(propType);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_WhenAutoIncrementingNull_ShouldReturnTrue()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            string propType = "Int32";
            DMProperty dmProperty = GetDMProperty(dmClass, propType);
            dmProperty.AutoIncrementing = null;
            IBusinessObjectRule rule = new DMPropertyAutoIncrementingMustBeOfTypeInt();
            //---------------Assert Precondition----------------
            Assert.IsNull(dmProperty.AutoIncrementing);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        private void SetAutoIncrementing(DMProperty dmProperty, bool incrementing)
        {
            dmProperty.AutoIncrementing = incrementing;
        }

        private DMProperty GetDMProperty(DMClass dmClass, string propType)
        {
            string name = TestUtilsShared.GetRandomString();
            return TestUtilsDMProperty.CreateDMProperty(dmClass, name, "System", propType);
        }

        #endregion

    }
}